package com.zhpan.indicator.base;

import com.zhpan.indicator.enums.IndicatorSlideMode;
import com.zhpan.indicator.options.IndicatorOptions;
import com.zhpan.indicator.utils.Log;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.components.PageSliderProvider;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * 指示器基类
 *
 * @since 2021-04-13
 */
public class BaseIndicatorComponent extends Component implements IIndicator {
    protected IndicatorOptions mIndicatorOptions;
    private PageSlider mPageSlide;
    private int pageSize;

    /**
     * 基类
     *
     * @param context 上下文
     */
    public BaseIndicatorComponent(Context context) {
        this(context, null);
    }

    /**
     * 基类
     *
     * @param context 上下文
     * @param attrSet 参数
     */
    public BaseIndicatorComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    /**
     * 基类
     *
     * @param context 上下文
     * @param attrSet 参数
     * @param styleName 样式
     */
    public BaseIndicatorComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        mIndicatorOptions = new IndicatorOptions(getContext());
    }

    @Override
    public void notifyDataChanged() {
        setupViewPager();
        pageSize = getPageSize();
        mIndicatorOptions.setRealPosition(mPageSlide.getCurrentPage());
        postLayout();
        invalidate();
    }

    @Override
    public void setIndicatorOptions(IndicatorOptions options) {
        mIndicatorOptions = options;
    }

    @Override
    public void onPageSliding(int position, float positionOffset, int offsetPixels) {
        if (getSlideMode() != IndicatorSlideMode.NORMAL && pageSize > 1) {
            if (offsetPixels < 0) {
                position = position - 1;
                positionOffset = 1 - positionOffset;
            }
            scrollSlider(position, positionOffset);
            invalidate();
        }
    }

    @Override
    public void onPageSlideStateChanged(int state) {
    }

    @Override
    public void onPageChosen(int position) {
        if (getSlideMode() == IndicatorSlideMode.NORMAL) {
            setCurrentPosition(position);
            setSlideProgress(0f);
            invalidate();
        }
    }

    /**
     * 无限轮播的时候 需要调用此方法
     * <p>
     * indicator.setCurrentPage();
     * pageSlide.setCurrentPage();
     *
     * @param position
     */
    public void setCurrentPage(int position) {
        if (position == 0) {
            setSlideProgress(0);
            setCurrentPosition(0);
        }
    }

    private void setupViewPager() {
        try {
            if (mPageSlide != null) {
                mPageSlide.addPageChangedListener(this);
                PageSliderProvider provider = mPageSlide.getProvider();
                if (provider != null) {
                    setPageSize(provider.getCount());
                }
            }
        } catch (IllegalStateException exception) {
            Log.e("Base", exception.getMessage());
        }
    }

    /**
     * 手指←←←←←←←← 滑动 offset 0 - 1 的状态
     * 手指→→→→→→→→ 滑动 offset 1 - 0 的变化
     *
     * @param position 是当前被拖动的位置
     * @param positionOffset positionOffset
     */
    private void scrollSlider(int position, float positionOffset) {
        int slideMode = mIndicatorOptions.getSlideMode();
        if (slideMode == IndicatorSlideMode.SCALE
            || slideMode == IndicatorSlideMode.COLOR) {
            setCurrentPosition(position);
            setSlideProgress(positionOffset);
        } else {
            if (position % getPageSize() == getPageSize() - 1) {
                if (positionOffset < 0.5) {
                    setCurrentPosition(position);
                    setSlideProgress(positionOffset);
                } else {
                    setCurrentPosition(0);
                    setSlideProgress(0f);
                }
            } else {
                setCurrentPosition(position);
                setSlideProgress(positionOffset);
            }
        }
    }

    /**
     * 绑定PageSlide
     *
     * @param pageSlider 卡片滑动器
     */
    public void setupWithPageSlide(PageSlider pageSlider) {
        this.mPageSlide = pageSlider;
        notifyDataChanged();
    }

    /**
     * 获取方向
     *
     * @return 返回方向
     */
    public int getOrientation() {
        return mIndicatorOptions.getOrientation();
    }

    /**
     * 设置方向
     *
     * @param orientation 方向
     */
    public void setOrientation(int orientation) {
        this.mIndicatorOptions.setOrientation(orientation);
    }

    /**
     * 获取指示器样式
     *
     * @return 样式
     */
    public int getIndicatorStyle() {
        return mIndicatorOptions.getIndicatorStyle();
    }

    /**
     * 设置样式
     *
     * @param indicatorStyle 样式
     */
    public void setIndicatorStyle(int indicatorStyle) {
        this.mIndicatorOptions.setIndicatorStyle(indicatorStyle);
    }

    /**
     * 获取滑动模式
     *
     * @return 滑动模式
     */
    public int getSlideMode() {
        return mIndicatorOptions.getSlideMode();
    }

    /**
     * 设置滑动模式
     *
     * @param slideMode 滑动模式
     */
    public void setSlideMode(int slideMode) {
        this.mIndicatorOptions.setSlideMode(slideMode);
    }

    /**
     * 获取页数
     *
     * @return 页数
     */
    public int getPageSize() {
        return mIndicatorOptions.getPageSize();
    }

    /**
     * 设置页数
     *
     * @param pageSize 页数
     */
    public void setPageSize(int pageSize) {
        this.mIndicatorOptions.setPageSize(pageSize);
    }

    /**
     * 获取未选中颜色
     *
     * @return 颜色
     */
    public Color getNormalSliderColor() {
        return mIndicatorOptions.getNormalSliderColor();
    }

    /**
     * 设置未选中颜色
     *
     * @param normalSliderColor 颜色
     */
    public void setNormalSliderColor(Color normalSliderColor) {
        this.mIndicatorOptions.setNormalSliderColor(normalSliderColor);
    }

    /**
     * 获取选中颜色
     *
     * @return 颜色
     */
    public Color getCheckedSliderColor() {
        return mIndicatorOptions.getCheckedSliderColor();
    }

    /**
     * 设置选中颜色
     *
     * @param checkedSliderColor 选中颜色
     */
    public void setCheckedSliderColor(Color checkedSliderColor) {
        this.mIndicatorOptions.setCheckedSliderColor(checkedSliderColor);
    }

    /**
     * 获取间隙
     *
     * @return 间隙
     */
    public float getSliderGap() {
        return mIndicatorOptions.getSliderGap();
    }

    /**
     * 设置间隙
     *
     * @param sliderGap 间隙
     */
    public void setSliderGap(float sliderGap) {
        this.mIndicatorOptions.setSliderGap(sliderGap);
    }

    /**
     * 获取高度
     *
     * @return 高度
     */
    public float getSliderHeight() {
        return mIndicatorOptions.getSliderHeight();
    }

    /**
     * 设置高度
     *
     * @param sliderHeight 高度
     */
    public void setSliderHeight(float sliderHeight) {
        this.mIndicatorOptions.setSliderHeight(sliderHeight);
    }

    /**
     * 设置宽度
     *
     * @param normalIndicatorWidth 未选中宽度
     * @param checkedIndicatorWidth 选中宽度
     */
    public void setSliderWidth(float normalIndicatorWidth, float checkedIndicatorWidth) {
        this.mIndicatorOptions.setSliderWidth(normalIndicatorWidth, checkedIndicatorWidth);
    }

    /**
     * 设置宽度
     *
     * @param width 宽度
     */
    public void setSliderWidth(float width) {
        setSliderWidth(width, width);
    }

    /**
     * 获取未选中宽度
     *
     * @return 获取未选中宽度
     */
    public float getNormalSliderWidth() {
        return mIndicatorOptions.getNormalSliderWidth();
    }

    /**
     * 设置未选中宽度
     *
     * @param normalSliderWidth 获取未选中宽度
     */
    public void setNormalSliderWidth(float normalSliderWidth) {
        this.mIndicatorOptions.setNormalSliderWidth(normalSliderWidth);
    }

    /**
     * 获取未选中宽度
     *
     * @return 宽度
     */
    public float getCheckedSliderWidth() {
        return mIndicatorOptions.getCheckedSliderWidth();
    }

    /**
     * 设置宽度
     *
     * @param checkedSliderWidth 宽度
     */
    public void setCheckedSliderWidth(float checkedSliderWidth) {
        this.mIndicatorOptions.setCheckedSliderWidth(checkedSliderWidth);
    }

    /**
     * 获取当前position
     *
     * @return 当前位置
     */
    public int getCurrentPosition() {
        return mIndicatorOptions.getCurrentPosition();
    }

    /**
     * 设置当前位置
     *
     * @param currentPosition 当前位置
     */
    public void setCurrentPosition(int currentPosition) {
        this.mIndicatorOptions.setCurrentPosition(currentPosition);
        this.mIndicatorOptions.setRealPosition(currentPosition);
    }

    /**
     * 获取当前进度
     *
     * @return 当前滑动进度
     */
    public float getSlideProgress() {
        return mIndicatorOptions.getSlideProgress();
    }

    /**
     * 设置当前滑动进度
     *
     * @param slideProgress 当前滑动进度
     */
    public void setSlideProgress(float slideProgress) {
        this.mIndicatorOptions.setSlideProgress(slideProgress);
    }
}
